import {
  createRouter,
  createWebHashHistory,
} from "vue-router";

import type { RouteRecordRaw, Router } from "vue-router";

/**
 * 加载路由
 * @param modules
 * @returns
 */
function loadModules(modules: any) {
  let moduleRoutes: Array<RouteRecordRaw> = [];
  for (const path in modules) {
    const module = modules[path];
    const defaultExport = module.default as Array<RouteRecordRaw>;
    moduleRoutes = moduleRoutes.concat(defaultExport);
  }
  return moduleRoutes;
}

// 内部布局 (/internal)
const internalModules = import.meta.glob(
  "./modules/internal/**/*.{js,ts}", { eager: true }
);
let internalLayout = loadModules(internalModules);
// 表单布局  (/form)
const formModules = import.meta.glob("./modules/form/**/*.{js,ts}", { eager: true });
let formLayout = loadModules(formModules);

// external
// externalWorkbench
// form
// internal

// 动态载入modules
let routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/newlogin"
  },
  {
    path: "/manageLogin",
    name: "from",
    component: () => import("@/views/FormLayout.vue"),
    redirect: '/manageLogin',
    children: [
      ...formLayout
    ]
  },
  {
    path:"/newlogin",
    name:"newlogin",
    component:()=>import("@/views/UserAccount/NewUserLogin.vue"),
    meta:{
      title:"登录",
      allowLogout:true
    }
},
  // 内部界面
  {
    path: "/internal",
    name: "internal",
    component: () => import("@/views/InternalLayout.vue"),
    redirect: '/internal/home',
    children: [
      ...internalLayout,
      {
        path: "/internal/404",
        name: "InternalNotFound",
        component: () => import("@/views/errors/404.vue"),
        meta: {
          title: "页面不存在"
        }
      },
      {
        path: "home",
        name: "InternalHome",
        component: () => import("@/views/internal/home.vue"),
        meta: {
          title: "工作中心"
        }
      },
      {
        path: "userSetting",
        name: "userSetting",
        component: () => import("@/views/internal/userSetting.vue"),
        meta: {
          title: "个人设置"
        }
      },
    ],
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
});

export default router;
